/*
 * 文件名：ContractExtController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年2月13日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.domain.BpaProjectDomain;
import top.infopub.mgr.bpa.domain.ContractExtSupDomain;
import top.infopub.mgr.bpa.jsonVo.ContractExtJsonVo;
import top.infopub.mgr.bpa.jsonVo.ContractExtSupJsonVo;
import top.infopub.mgr.bpa.service.BpaContractBpaReviewService;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.ContractExtSupService;
import top.infopub.mgr.bpa.vo.ContractExtSupVo;
import top.infopub.mgr.cap.jsonvo.LookInfoJsonVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.model.bpa.BpaContractListing;
import top.infopub.model.bpa.ContractExt;
import top.infopub.model.bpa.ContractExtSup;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**对外合同
 * @since商务管理/对外合同/承继协议/补充协议
 */
@Controller
@RequestMapping("/contractExtSup")
public class ContractExtSupController extends GenericFlowController {


    @Autowired
    private ContractExtSupService contractExtSupService;
    @Autowired
    private BpaContractBpaReviewService bpaContractBpaReviewService;

    @Autowired
    private ProProcessManageService proProcessManageService;
    @Autowired
    private BpaProcessManageService bpaProcessManageService;
    /**
     * 补充协议列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<ContractExtSupDomain> queryDataByCondition(ContractExtSupVo contractExtSupVo,
                                                            ParamPage paramPage) throws ParseException {
        Page<ContractExtSupDomain> page = new Page<ContractExtSupDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = contractExtSupService.selectByDomainWhere(page, contractExtSupVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //审批
        bpaProcessManageService.auditProject(id, processId, status, auditMsg);
        return RspVo.getSuccessResponse();
    }

    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String id) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id",id);
        ContractExtSup contractExtSup = contractExtSupService.selectBySguid(id);
        request.setAttribute("isAuditFlag", "1");
        if (contractExtSup!=null){
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), contractExtSup.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(contractExtSup.getProcessId()) && contractExtSup.getProcessId().length() > 10){
                request.setAttribute("processId", contractExtSup.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(contractExtSup.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), contractExtSup.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        return "bpa/lay-SuccessionSupplementaryAgreement-detail";
    }

    /**项目下拉**/
    @RequestMapping("/queryDrop")
    @ResponseBody
    public RspVo queryDrop(){
        //查询已有承继协议的项目
        List<BpaProjectDomain> bpaProjectDomain = contractExtSupService.selectProject();
        return RspVo.getSuccessResponse(bpaProjectDomain);
    }

    /**数据枚举**/
    @RequestMapping("/getDrop")
    @ResponseBody
    public RspVo getDrop(){
        LookInfoJsonVo lookInfoJsonVo = new LookInfoJsonVo();
        return RspVo.getSuccessResponse(lookInfoJsonVo);
    }

    /**详情**/
    @RequestMapping("/queryExtDetail")
    @ResponseBody
    public RspVo queryExtDetail(String id) {
        //承继协议
        ContractExtSupJsonVo contractExtSupJsonVo = contractExtSupService.queryDetailById(id);
        return RspVo.getSuccessResponse(contractExtSupJsonVo);
    }

    /**保存**/
    @RequestMapping("save")
    @ResponseBody
    public RspVo save (String bodyStr, Integer submitType){
        AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        ContractExtSupJsonVo vo = JSON.parseObject(s,ContractExtSupJsonVo.class);
        ContractExtSup contractExtSup = new ContractExtSup();
        if (StringUtils.isNotBlank(vo.getContractExtSup().getId())){//编辑
            BeanUtils.copyProperties(vo.getContractExtSup(),contractExtSup);
            contractExtSup.setUptBy(userDtl.getId());
            contractExtSup.setUptTime(new Date());
            contractExtSup.setProCode(vo.getProjectInfo().getProCode());
        }else{//新增
            BeanUtils.copyProperties(vo.getContractExtSup(),contractExtSup);
            contractExtSup.setId(IdFactory.getUUIDUpper());
            contractExtSup.setProCode(vo.getProjectInfo().getProCode());
            contractExtSup.setAddBy(userDtl.getId());
            contractExtSup.setAddTime(new Date());
        }
        /**
         * 附件处理
         * */
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(vo.getFileAtt()!=null){
            String[] fileAtt = vo.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(userDtl,contractExtSup.getProCode(),file);
                attachmentList.add(attachment);
            }
        }
        return contractExtSupService.saveAll(userDtl,bodyStr,submitType,contractExtSup,vo.getBpaContractReviewPublic(),vo.getBpaContractSummary(),
                vo.getBpaContractListing(),vo.getBpaContractFormula(),vo.getBpaContractBid(),vo.getBpaContractBusiness(),attachmentList
        );
    }

    /**子项**/
    @RequestMapping("/saveListing")
    @ResponseBody
    public RspVo saveListing(String bodyStr,String id){
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        List<BpaContractListing> listingJsonVos = JSON.parseArray(s, BpaContractListing.class);
        return bpaContractBpaReviewService.saveListing(listingJsonVos,id);
    }
}
